#include <math.h>
#include <stdio.h>
#include <wx/wx.h>
#include <wx/string.h>
#include "mycanvas.h"
#include "topFrame.h"
#include "header.h"

#include "start.xpm"
#include "stop.xpm"


 BEGIN_EVENT_TABLE(TopFrame, wxFrame)
     EVT_MENU(wxID_EXIT,  TopFrame::OnQuit)
     EVT_CLOSE(TopFrame::OnQuitX)
     EVT_MENU(ID_START,  TopFrame::OnStart)
     EVT_MENU(ID_STOP,  TopFrame::OnStop)

 END_EVENT_TABLE()




TopFrame::TopFrame(SampleApp * AApp, wxString Title) : wxFrame(NULL, wxID_ANY, Title){

    App = AApp;

    //creamos un menu muy simple
    wxMenu *fileMenu = new wxMenu;
    fileMenu->Append(wxID_EXIT, _T("&Exit\tAlt-F4"), _T("Termina el programa"));
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, _T("&File"));
    SetMenuBar(menuBar);
    // Panel de herramientas (selector de vista)
    wxPanel * ControlPanel = new wxPanel(this, wxID_ANY, wxPoint(0,0), wxSize(256, 32));
    ProgressBar = new wxGauge(ControlPanel, wxID_ANY, 100, wxPoint(3,3), wxSize(250,32));
    MyCanvas * drawPanel = new MyCanvas(App,this, wxID_ANY, wxPoint(0,35), wxSize(640 , 480), wxTAB_TRAVERSAL | wxSUNKEN_BORDER);
    m_drawPanel = drawPanel;


    //toolbar
    wxToolBar *toolBar = CreateToolBar();//nueva toolbar(constructor)
    toolBar->SetMargins(5, 5);//margenes
    toolBar->SetToolBitmapSize(wxSize(32, 32));//recoratara las imagenes a 32x32

    toolBar->AddTool(ID_START, wxBITMAP(start), wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, (wxObject *)NULL, _T("Start"), _T("Run the progress bar"));
    toolBar->AddTool(ID_STOP, wxBITMAP(stop), wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, (wxObject *)NULL, _T("Start"), _T("Run the progress bar"));


    wxStaticText * aLabel = new wxStaticText(<<DatePanel>>, wxID_ANY, _T("This is a wxGauge"),
                              wxPoint(250, 60), wxDefaultSize,
                              wxALIGN_CENTRE /*| wxST_NO_AUTORESIZE*/);
    aLabel->SetForegroundColour( *wxBLACK );

	wxStaticText * bLabel = new wxStaticText(<<DatePanel>>, wxID_ANY, _T("This is 2 wxTextCtrl"),
                              wxPoint(250, 60), wxDefaultSize,
                              wxALIGN_CENTRE /*| wxST_NO_AUTORESIZE*/);
    bLabel->SetForegroundColour( *wxBLACK );


    // validator that only accept number keys
	wxTextValidator OnlyNum = wxTextValidator(wxFILTER_NUMERIC);
   // text box that accepts all
	wxTextCtrl * aText = new wxTextCtrl(<<DatePanel>>, wxID_ANY, _T("Any text"), wxDefaultPosition, wxSize(100, 20), wxTE_RIGHT);
   // text box that only take numbers
	wxTextCtrl * bText = new wxTextCtrl(<<DatePanel>>, wxID_ANY, _T("1"), wxDefaultPosition, wxSize(100, 20), wxTE_RIGHT, OnlyNum);



    m_running = false;

    toolBar->Realize();

}


void TopFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 {
     Quit();
 }

void TopFrame::Quit()
 {
     // true is to force the frame to close
     Close(true);
 }

void TopFrame::OnQuitX(wxCloseEvent& WXUNUSED(event))
 {
     /**************************************/
     /***** | Aqui todas las ordenes | *****/
     /***** |  previas al cerrado    | *****/
     /******V************************V******/
     // Por ejemplo
     // remove( "temporal.tmp" );
     /******A************************A******/
     /***** | Aqui todas las ordenes | *****/
     /***** |  previas al cerrado    | *****/
     /**************************************/

     Destroy();
 }

void TopFrame::OnStart(wxCommandEvent& WXUNUSED(event))
{
    int i;
    bool token_true;

    m_running = true;

    while(m_running)
    {
        for(i=1;i<=100;i++)
        {
            ProgressBar->SetValue(i);
            while((TopFrame *) App->Pending())
            {
                token_true=(TopFrame *) App->Dispatch();
            }
        }
        for(i=99;i>=2;i--)
        {
            ProgressBar->SetValue(i);
            while((TopFrame *) App->Pending())
            {
                token_true=(TopFrame *) App->Dispatch();
            }
        }
    }
}


void TopFrame::OnStop(wxCommandEvent& WXUNUSED(event))
{
    m_running = false;
}
